/*
 * adc.cpp
 *
 *  Created on: 22.02.2012
 *      Author: Roma Jam
 */
#include "adc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"

void adc_t::Init(void) {
    GPIOs_Set();
    SPI_InitTypeDef   SPI_InitStructure;
    RCC_APB2PeriphClockCmd(SPIa_CLK | SPIa_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPIa, &SPI_InitStructure);
    SPI_Cmd(SPIa, ENABLE);

    RCC_APB1PeriphClockCmd(SPIb_CLK | SPIb_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPIb, &SPI_InitStructure);
    SPI_Cmd(SPIb, ENABLE);
} // Init

void adc_t::GPIOs_Set(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = SPIa_PIN_SCK ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(SPIa_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = SPIb_PIN_SCK ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(SPIb_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = SPIa_PIN_MISO;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SPIa_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = SPIb_PIN_MISO;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SPIb_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin  = SPIa_CS;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(SPIa_GPIO, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin  = SPIb_CS;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(SPIb_GPIO, &GPIO_InitStructure);
    SPIa_CS_HI();
    SPIb_CS_HI();
} // GPIOs Set

uint16_t adc_t::Recieve_chann01(void) {
    uint32_t tmp=0;

    SPIa_CS_LO();
    SPIa->DR = 0x00;
    while (!(SPIa->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp = SPIa->DR;
    tmp <<=8;
    SPIa->DR = 0x00;
    while (!(SPIa->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp += SPIa->DR;
    tmp <<= 8;
    SPIa->DR = 0x00;
    while (!(SPIa->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp += SPIa->DR;
    SPIa_CS_HI();
    tmp >>= 2;
    return (uint16_t)(tmp & 0x0000FFFF);
} // Recieve chann01
uint16_t adc_t::Recieve_chann02(void) {
    uint32_t tmp=0;

    SPIb_CS_LO();
    SPIb->DR = 0x00;
    while (!(SPIb->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp = SPIb->DR;
    tmp <<=8;
    SPIb->DR = 0x00;
    while (!(SPIb->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp += SPIb->DR;
    tmp <<= 8;
    SPIb->DR = 0x00;
    while (!(SPIb->SR & SPI_I2S_FLAG_RXNE));  // Wait for SPI transmission to complete
    tmp += SPIb->DR;
    SPIb_CS_HI();
    tmp >>= 2;
    return (uint16_t)(tmp & 0x0000FFFF);
}

uint16_t adc_t::to_AngleAZ(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    ADC_var.Azumuth_below_0 = true;
    if (ANumber < Az_norm) {
        temporaryNumber = Az_norm - ANumber;
        ADC_var.Azumuth_below_0 = false;
    }
    else temporaryNumber = ANumber - Az_norm;
    temporaryNumber /= 20;
    temporaryNumber /= 10;
    return temporaryNumber;
}

uint16_t adc_t::to_AAZ_Lab(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    ADC_var.Azumuth_below_0 = true;
    if (ANumber < Az_norm) {
        temporaryNumber = Az_norm - ANumber;
        ADC_var.Azumuth_below_0 = false;
        temporaryNumber /= 20;
        temporaryNumber /= 10;
        return temporaryNumber;
    }
    else {
        temporaryNumber = ANumber - Az_norm;
        temporaryNumber /= 20;
        temporaryNumber /= 10;
        temporaryNumber = 256 - temporaryNumber;
        return temporaryNumber;
    }
}


uint16_t adc_t::to_MinuteAZ(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    if (ANumber > Az_norm) {
        temporaryNumber = Az_norm - ANumber;
    }
    else temporaryNumber = ANumber - Az_norm;
    temporaryNumber /= 20;
    temporaryNumber %= 10;
    temporaryNumber *= 6;
    return temporaryNumber;
}

uint16_t adc_t::to_AngleEL(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    ADC_var.Elevation_below_0 = false;
    if (ANumber < El_norm) {
        temporaryNumber = El_norm - ANumber;
        ADC_var.Elevation_below_0 = true;
    }
    else temporaryNumber = ANumber - El_norm;
    temporaryNumber /= 14;
    temporaryNumber /= 10;
    return temporaryNumber;
}

uint16_t adc_t::to_AEL_Lab(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    ADC_var.Elevation_below_0 = true;
    if (ANumber < El_norm) {
        temporaryNumber = El_norm - ANumber;
        ADC_var.Elevation_below_0 = false;
        temporaryNumber /= 14;
        temporaryNumber /= 10;
        return temporaryNumber;
    }
    else {
        temporaryNumber = ANumber - El_norm;
        temporaryNumber /= 14;
        temporaryNumber /= 10;
        temporaryNumber = 256 - temporaryNumber;
        return temporaryNumber;
    }
}

uint16_t adc_t::to_MinuteEL(const uint16_t ANumber) {
    uint16_t temporaryNumber = 0;
    if (ANumber < El_norm) {
        temporaryNumber = El_norm - ANumber;
    }
    else temporaryNumber = ANumber - El_norm;
    temporaryNumber /= 14;
    temporaryNumber %= 10;
    temporaryNumber *= 6;
    return temporaryNumber;
}

uint16_t adc_t::to_ADC_Az(uint16_t ANumber) {
    ANumber = 0;
    ANumber = (ANumber*240)+Az_norm;
    return ANumber;
}
uint16_t adc_t::to_ADC_El(uint16_t ANumber) {
    ANumber = 0;
    ANumber = (ANumber*140)+El_norm;
    return ANumber;
}

uint16_t adc_t::Char_to_Uint(uint8_t Symbol) {
    return ((Symbol) - 0x30);
}
